package ipc

import (
	"github.com/golang/protobuf/proto"
)

type IpcClient struct {
	conn chan Request
	back chan error
}


func NewIpcClient(server *IpcServer) *IpcClient {
	conn, back := server.Connect()

	return &IpcClient{conn, back}
}

func (client *IpcClient) Call(uid int32, cmd uint16, pbMsg proto.Message) (err error) {
	req := Request{uid, cmd,  pbMsg, false}

	//var b []byte
	//b, err = json.Marshal(req)
	//if err != nil {
	//	return
	//}

	client.conn <- req

	if (req.AskBack) {
		err = <- client.back   // 等待返回
	}

	//var resp1 Response
	//err = json.Unmarshal([]byte(str), &resp1)
	//resp = &resp1

	return
}

func (client *IpcClient) Close() {
	client.conn <- Request{0, 0, nil, false}
}